package com.sc.smartcommunity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.smartcommunity.domain.User;
import com.sc.smartcommunity.mapper.UserMapper;
import com.sc.smartcommunity.service.UserService;
import org.springframework.stereotype.Service;

/**
 * 用户服务实现类
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public boolean register(String username, String phone, String password) {
        // 检查用户名或手机号是否已存在
        long count = this.count(new LambdaQueryWrapper<User>()
                .eq(User::getUsername, username)
                .or()
                .eq(User::getPhone, phone));
        if (count > 0) {
            return false;
        }

        // 保存用户
        User user = new User();
        user.setUsername(username);
        user.setPhone(phone);
        user.setPassword(password);
        return this.save(user);
    }

    @Override
    public boolean login(String phone, String password) {
        User user = this.getOne(new LambdaQueryWrapper<User>()
                .eq(User::getPhone, phone)
                .eq(User::getPassword, password));
        return user != null;
    }

    @Override
    public User getUserInfo(String loginId) {
        return this.getOne(new LambdaQueryWrapper<User>()
                .eq(User::getPhone, loginId));
    }


    @Override
    public boolean updateUser(User user) {
        return this.updateById(user);
    }
}
